package offer;

public class _14_剪绳子 {
    public static void main(String[] args) {
        Solution solution = new _14_剪绳子().new Solution();
        System.out.println(solution.cuttingRope(120) % 1000000007);
    }

    class Solution {
        public long cuttingRope(int n) {
            if (n == 0) return 0;
            if (n == 1) return 0;
            long[] dp = new long[n + 1];
            dp[0] = 0;
            dp[1] = 0;
            for (int i = 2; i <= n; i++) {
                long curMax = 0;
                for (int j = 1; j < i; j++) {
                    curMax = Math.max(curMax, Math.max(j * (i - j), j * dp[i - j]));
                }
                dp[i] = curMax;
            }
            return dp[n];
        }
    }

    // 不取余
    class Solution2 {
        public int cuttingRope(int n) {
            if (n == 0) return 0;
            if (n == 1) return 0;
            int[] dp = new int[n + 1];
            dp[0] = 0;
            dp[1] = 0;
            for (int i = 2; i <= n; i++) {
                int curMax = 0;
                for (int j = 1; j < i; j++) {
                    curMax = Math.max(curMax, Math.max(j * (i - j), j * dp[i - j]));
                }
                dp[i] = curMax;
            }
            return dp[n];
        }
    }


    // 超时了
    class Solution1 {
        public int cuttingRope(int n) {
            if (n == 1) {
                return 1;
            }
            int max = Integer.MIN_VALUE;
            for (int i = 1; i < n; i++) {
                max = Math.max(max, i * cut(n - i));
            }
            return max;
        }

        private int cut(int n) {
            if (n == 1) {
                return 1;
            }
            int max = Integer.MIN_VALUE;
            for (int i = 1; i < n; i++) {
                max = Math.max(max, i * cut(n - i));
            }
            return Math.max(max, n);
        }
    }
}
